package com.runyu.cordova.plugin.crypto;

import java.util.ArrayList;
import java.util.List;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

import com.runyu.cordova.plugin.JsActionSupport;

import android.content.Context;

public class ApplicationStarter implements JsActionSupport {

	private CordovaWebView webView;

	private CordovaInterface cordova;

	private static final String ACTION_START_JS_ACTION = "startJsAction";

	public ApplicationStarter(CordovaWebView webView, CordovaInterface cordova) {
		this.webView = webView;
		this.cordova = cordova;
	}

	public boolean startApp() {
		webView.getEngine().evaluateJavascript(ACTION_START_JS_ACTION, null);
		Context applicationContext = cordova.getContext().getApplicationContext();
		return NativeLicense.startApp(this.webView, applicationContext);
	}

	@Override
	public List<String> getSupportAction() {
		List<String> actions = new ArrayList<>();
		actions.add(ACTION_START_JS_ACTION);
		return actions;
	}

	@Override
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		cordova.getActivity().runOnUiThread(new Runnable() {
			@Override
			public void run() {
				NativeLicense.startApp(webView, cordova.getContext().getApplicationContext());
			}
		});
		return true;
	}

}
